home *** CD-ROM | disk | FTP | other *** search
/ NeXTSTEP 3.3 (Developer)…68k, x86, SPARC, PA-RISC] / NeXTSTEP 3.3 Dev Intel.iso / NextDeveloper / Source / GNU / libg++ / libio / gen-params < prev    next >
Text File  |  1994-02-15  |  14KB  |  446 lines

  1. #!/bin/sh
  2. # Copyright (C) 1992, 1993 Free Software Foundation
  3. # This file is part of the GNU IO Library.  This library is free
  4. # software; you can redistribute it and/or modify it under the
  5. # terms of the GNU General Public License as published by the
  6. # Free Software Foundation; either version 2, or (at your option)
  7. # any later version.
  8. # This library is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11. # GNU General Public License for more details.
  12. # You should have received a copy of the GNU General Public License
  13. # along with GNU CC; see the file COPYING.  If not, write to
  14. # the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  15.  
  16. #    Written by Per Bothner (bothner@cygnus.com)
  17.  
  18. # This is a shell-script that figures out various things about a
  19. # system, and writes (to stdout) a C-style include files with
  20. # suitable definitions, including all the standard Posix types.
  21. # It works by compiling various test programs -- some are run through
  22. # the C pre-processor, and the output examined.
  23. # The test programs are only compiled, not executed, so the script
  24. # should even if you're cross-compiling.
  25. # It uses $CC (which defaults to cc) to compile C programs (extension .c),
  26. # and $CXX (which defaults to gcc) to compile C++ programs (extension .C).
  27. # The shell-script is written for libg++.a.
  28.  
  29. # Usage: gen-params [NAME1=name1 ...]
  30. # - where an assignment (such as size_t="unsigned int" means to
  31. # use that value, instead of trying to figure it out.
  32.  
  33. # Uncomment following line for debugging
  34. # set -x
  35.  
  36. SED=sed
  37.  
  38. # Evaluate the arguments (which should be assignments):
  39. for arg in "$@"; do
  40.   # Quote arg (i.e. FOO=bar => FOO='bar'), then eval it.
  41.   eval `echo "$arg" | ${SED} -e "s|^\(.*\)=\(.*\)|\1='\2'|"`
  42. done
  43.  
  44. macro_prefix=${macro_prefix-"_G_"}
  45. rootdir=`pwd`/..
  46. gccdir=${gccdir-${rootdir}/gcc}
  47. binutilsdir=${binutilsdir-${rootdir}/binutils}
  48. CC=${CC-`if [ -f ${gccdir}/xgcc ] ; \
  49.     then echo ${gccdir}/xgcc -B${gccdir}/ -I${gccdir}/include ; \
  50.     else echo cc ; fi`}
  51. CXX=${CXX-`if [ -f ${gccdir}/xgcc ] ; \
  52.     then echo ${gccdir}/xgcc -B${gccdir}/ -I${gccdir}/include ; \
  53.     else echo gcc ; fi`}
  54. CPP=${CPP-`echo ${CC} -E`}
  55. CONFIG_NM=${CONFIG_NM-`if [ -f ${binutilsdir}/nm ] ; \
  56.     then echo ${binutilsdir}/nm ; \
  57.     else echo nm ; fi`}
  58.  
  59. cat <<!EOF!
  60. /* AUTOMATICALLY GENERATED; DO NOT EDIT! */ 
  61. #ifndef ${macro_prefix}config_h
  62. #define ${macro_prefix}config_h
  63. !EOF!
  64.  
  65. if [ x"${LIB_VERSION}" != "x" ] ; then
  66.   echo "#define ${macro_prefix}LIB_VERSION" '"'${LIB_VERSION}'"'
  67. fi
  68.  
  69. # This program is used to test if the compiler prepends '_' before identifiers.
  70. # It is also used to check the g++ uses '$' or '.' various places.
  71.  
  72. if test -z "${NAMES_HAVE_UNDERSCORE}" -o -z "${DOLLAR_IN_LABEL}" ; then
  73.   cat >dummy.C <<!EOF!
  74.   struct filebuf {
  75.       virtual int foo();
  76.   };
  77.   filebuf ff;
  78.   extern "C" int FUNC(int);
  79.   int FUNC(int i) { return i+10; }
  80. !EOF!
  81.  
  82.   if ${CXX} -c dummy.C ; then
  83.     if test -n "${NAMES_HAVE_UNDERSCORE}" ; then
  84.      echo "#define ${macro_prefix}NAMES_HAVE_UNDERSCORE ${NAMES_HAVE_UNDERSCORE}"
  85.     elif test "`${CONFIG_NM} dummy.o | grep _FUNC`" != ""; then
  86.       echo "#define ${macro_prefix}NAMES_HAVE_UNDERSCORE 1"
  87.     elif test "`${CONFIG_NM} dummy.o | grep FUNC`" != ""; then
  88.       echo "#define ${macro_prefix}NAMES_HAVE_UNDERSCORE 0"
  89.     else
  90.       echo "${CONFIG_NM} failed to find FUNC in dummy.o!" 1>&2; exit -1;
  91.     fi
  92.  
  93. #    if test -n "${DOLLAR_IN_LABEL}" ; then
  94. #      echo "#define ${macro_prefix}DOLLAR_IN_LABEL ${DOLLAR_IN_LABEL}"
  95. #    elif test "`${CONFIG_NM} dummy.o | grep 'vt[$$]7filebuf'`" != ""; then
  96. #      echo "#define ${macro_prefix}DOLLAR_IN_LABEL 1"
  97. #    elif test "`${CONFIG_NM} dummy.o | grep 'vt[.]7filebuf'`" != ""; then
  98. #      echo "#define ${macro_prefix}DOLLAR_IN_LABEL 0"
  99. #    elif test "`${CONFIG_NM} dummy.o | grep 'vtbl__7filebuf'`" != ""; then
  100. #      echo "#define ${macro_prefix}DOLLAR_IN_LABEL 0"
  101. #    else
  102. #      echo "gen-params: ${CONFIG_NM} failed to find vt[.\$]filebuf in dummy.o!" 1>&2; exit 1
  103. #    fi
  104.   else
  105.     # The compile failed for some reason (no C++?)
  106.     echo "gen-params: could not compile dummy.C with ${CXX}" 1>&2; exit 1;
  107.   fi
  108. fi
  109.  
  110. # A little test program to check if struct stat has st_blksize.
  111. cat >dummy.c <<!EOF!
  112. #include <sys/types.h>
  113. #include <sys/stat.h>
  114. int BLKSIZE(struct stat *st)
  115. {
  116.     return st->st_blksize;
  117. }
  118. !EOF!
  119.  
  120. if ${CC} -c dummy.c >/dev/null 2>&1 ; then
  121.   echo "#define ${macro_prefix}HAVE_ST_BLKSIZE 1"
  122. else
  123.   echo "#define ${macro_prefix}HAVE_ST_BLKSIZE 0"
  124. fi
  125.  
  126. # Next, generate definitions for the standard types (such as mode_t)
  127. # compatible with those in the standard C header files.
  128. # It works by a dummy program through the C pre-processor, and then
  129. # using sed to search for typedefs in the output.
  130.  
  131. cat >dummy.c <<!EOF!
  132. #include <sys/types.h>
  133. #include <stddef.h>
  134. #include <stdarg.h>
  135. #include <stdio.h>
  136. #include <time.h>
  137. #include <signal.h>
  138. #ifdef size_t
  139. typedef size_t Xsize_t;
  140. #elif defined(__SIZE_TYPE__)
  141. typedef __SIZE_TYPE__ Xsize_t;
  142. #endif
  143. #ifdef ptrdiff_t
  144. typedef ptrdiff_t Xptrdiff_t;
  145. #elif defined(__PTRDIFF_TYPE__)
  146. typedef __PTRDIFF_TYPE__ Xptrdiff_t;
  147. #endif
  148. #ifdef wchar_t
  149. typedef wchar_t Xwchar_t;
  150. #elif defined(__WCHAR_TYPE__)
  151. typedef __WCHAR_TYPE__ Xwchar_t;
  152. #endif
  153. #ifdef va_list
  154. typedef va_list XXXva_list;
  155. #endif
  156. #ifdef BUFSIZ
  157. int XBUFSIZ=BUFSIZ;
  158. #endif
  159. #ifdef FOPEN_MAX
  160. int XFOPEN_MAX=FOPEN_MAX;
  161. #endif
  162. #ifdef FILENAME_MAX
  163. int XFILENAME_MAX=FILENAME_MAX;
  164. #endif
  165. !EOF!
  166.  
  167. if ${CPP} dummy.c >TMP ; then true
  168. else
  169.   echo "gen-params: could not invoke ${CPP} on dummy.c" 1>&2 ; exit 1
  170. fi
  171. tr '    ' ' ' <TMP >dummy.out
  172.  
  173. for TYPE in dev_t clock_t fpos_t gid_t ino_t mode_t nlink_t off_t pid_t ptrdiff_t sigset_t size_t ssize_t time_t uid_t va_list wchar_t int32_t uint_32_t ; do
  174.     IMPORTED=`eval 'echo $'"$TYPE"`
  175.     if [ -n "${IMPORTED}" ] ; then
  176.     eval "$TYPE='$IMPORTED"
  177.     else
  178.     # Search dummy.out for a typedef for $TYPE, and write it out
  179.     # to TMP in #define syntax.
  180.     rm -f TMP
  181.     ${SED} -n -e "s|.*typedef  *\(.*\) X*$TYPE *;.*|\1|w TMP" \
  182.       <dummy.out>/dev/null
  183.     # Now select the first definition.
  184.         if [ -s TMP ]; then
  185.         # VALUE is now the typedef'd definition of $TYPE.
  186.             eval "VALUE='`${SED} -e 's| *$||' -e '2,$d' <TMP`'"
  187.         # Unless VALUE contains a blank, look for a typedef for it
  188.         # in turn (this could be a loop, but that would be over-kill).
  189.         if echo $VALUE | grep " " >/dev/null ; then true
  190.         else
  191.         rm -f TMP
  192.         ${SED} -n -e "s|.*typedef[     ][     ]*\(.*[^a-zA-Z0-9_]\)${VALUE}[     ]*;.*|\1|w TMP" <dummy.out>/dev/null
  193.         if [ -s TMP ]; then
  194.             eval "VALUE='`${SED} -e '2,$d' -e 's|[     ]*$||' <TMP`'"
  195.         fi
  196.         fi
  197.         eval "$TYPE='$VALUE'"
  198.     fi
  199.     fi
  200. done
  201.  
  202. cat <<!EOF!
  203. typedef ${clock_t-int /* default */} ${macro_prefix}clock_t;
  204. typedef ${dev_t-int /* default */} ${macro_prefix}dev_t;
  205. typedef ${fpos_t-long /* default */} ${macro_prefix}fpos_t;
  206. typedef ${gid_t-int /* default */} ${macro_prefix}gid_t;
  207. typedef ${ino_t-int /* default */} ${macro_prefix}ino_t;
  208. typedef ${mode_t-int /* default */} ${macro_prefix}mode_t;
  209. typedef ${nlink_t-int /* default */} ${macro_prefix}nlink_t;
  210. typedef ${off_t-long /* default */} ${macro_prefix}off_t;
  211. typedef ${pid_t-int /* default */} ${macro_prefix}pid_t;
  212. typedef ${ptrdiff_t-long int /* default */} ${macro_prefix}ptrdiff_t;
  213. typedef ${sigset_t-int /* default */} ${macro_prefix}sigset_t;
  214. typedef ${size_t-unsigned long /* default */} ${macro_prefix}size_t;
  215. typedef ${time_t-int /* default */} ${macro_prefix}time_t;
  216. typedef ${uid_t-int /* default */} ${macro_prefix}uid_t;
  217. typedef ${wchar_t-int /* default */} ${macro_prefix}wchar_t;
  218. typedef ${int32_t-int /* default */} ${macro_prefix}int32_t;
  219. typedef ${uint32_t-unsigned int /* default */} ${macro_prefix}uint32_t;
  220. !EOF!
  221.  
  222.  
  223. # ssize_t is the signed version of size_t
  224. if [ -n "${ssize_t}" ] ; then
  225.     echo "typedef ${ssize_t} ${macro_prefix}ssize_t;"
  226. elif [ -z "${size_t}" ] ; then
  227.     echo "typedef long ${macro_prefix}ssize_t;"
  228. else
  229.     # Remove "unsigned" from ${size_t} to get ${ssize_t}.
  230.     tmp="`echo ${size_t} | ${SED} -e 's|unsigned||g' -e 's|  | |g'`"
  231.     if [ -z "$tmp" ] ; then
  232.     tmp=int
  233.     else
  234.     # check $tmp doesn't conflict with <unistd.h>
  235.     echo "#include <unistd.h>
  236.     extern $tmp read();" >dummy.c
  237.     ${CC} -c dummy.c >/dev/null 2>&1 || tmp=int
  238.     fi
  239.     echo "typedef $tmp /* default */ ${macro_prefix}ssize_t;"
  240. fi
  241.  
  242. # va_list can cause problems (e.g. some systems have va_list as a struct).
  243. # Check to see if ${va_list-char*} really is compatible with stdarg.h.
  244. cat >dummy.C <<!EOF!
  245. #define X_va_list ${va_list-char* /* default */}
  246. extern long foo(X_va_list ap); /* Check that X_va_list compiles on its own */
  247. extern "C" {
  248. #include <stdarg.h>
  249. }
  250. long foo(X_va_list ap) { return va_arg(ap, long); }
  251. long bar(int i, ...)
  252. { va_list ap; long j; va_start(ap, i); j = foo(ap); va_end(ap); return j; }
  253. !EOF!
  254. if ${CXX} -c dummy.C >/dev/null 2>&1 ; then
  255.   # Ok: We have something that works.
  256.   echo "typedef ${va_list-char* /* default */} ${macro_prefix}va_list;"
  257. else
  258.   # No, it breaks.  Indicate that <stdarg.h> must be included.
  259.   echo "#define ${macro_prefix}NEED_STDARG_H
  260. #define ${macro_prefix}va_list va_list"
  261. fi
  262.  
  263. cat >dummy.c <<!EOF!
  264. #include <signal.h>
  265. extern int (*signal())();
  266. extern int dummy (int);
  267. main()
  268. {
  269.     int (*oldsig)(int) = signal (1, dummy);
  270.     (void) signal (2, oldsig);
  271.     return 0;
  272. }
  273. !EOF!
  274. if ${CC} -c dummy.c >/dev/null 2>&1 ; then
  275.   echo "#define ${macro_prefix}signal_return_type int"
  276. else
  277.   echo "#define ${macro_prefix}signal_return_type void"
  278. fi
  279.  
  280. # check sprintf return type
  281.  
  282. cat >dummy.c <<!EOF!
  283. #include <stdio.h>
  284. extern int sprintf(); char buf[100];
  285. int main() { return sprintf(buf, "%d", 34); }
  286. !EOF!
  287. if ${CC} -c dummy.c >/dev/null 2>&1 ; then
  288.   echo "#define ${macro_prefix}sprintf_return_type int"
  289. else
  290.   echo "#define ${macro_prefix}sprintf_return_type char*"
  291. fi
  292.  
  293. # Look for some standard macros
  294. for NAME in BUFSIZ FOPEN_MAX FILENAME_MAX NULL ; do
  295.     IMPORTED=`eval 'echo $'"$NAME"`
  296.     if [ -n "${IMPORTED}" ] ; then
  297.     eval "$NAME='$IMPORTED /* specified */"
  298.     else
  299.     rm -f TMP
  300.     ${SED} -n -e "s|int X${NAME}=\(.*\);|\1|w TMP" <dummy.out>/dev/null
  301.     # Now select the first definition.
  302.     if [ -s TMP ]; then
  303.         eval "$NAME='"`${SED} -e '2,$d' <TMP`"'"
  304.     fi
  305.     fi
  306. done
  307.  
  308. cat <<!EOF!
  309. #define ${macro_prefix}BUFSIZ ${BUFSIZ-1024 /* default */}
  310. #define ${macro_prefix}FOPEN_MAX ${FOPEN_MAX-32 /* default */}
  311. #define ${macro_prefix}FILENAME_MAX ${FILENAME_MAX-1024 /* default */}
  312. #define ${macro_prefix}NULL ${NULL-0 /* default */}
  313. #define ${macro_prefix}ARGS(ARGLIST) (...)
  314. !EOF!
  315.  
  316. rm -f dummy.c dummy.o
  317.  
  318. if test -n "${HAVE_ATEXIT}" ; then
  319.  echo "#define ${macro_prefix}HAVE_ATEXIT ${HAVE_ATEXIT}"
  320. else
  321.   cat >dummy.c <<!EOF!
  322. #include <stdlib.h>
  323. int main()
  324. {
  325.   atexit (0);
  326. }
  327. !EOF!
  328.   if ${CC} dummy.c >/dev/null 2>&1 ; then
  329.     echo "#define ${macro_prefix}HAVE_ATEXIT 1"
  330.   else
  331.     echo "#define ${macro_prefix}HAVE_ATEXIT 0"
  332.   fi
  333. fi
  334.  
  335.  
  336. # *** Check for presence of certain include files ***
  337.  
  338. # check for sys/resource.h
  339.  
  340. if test -n "${HAVE_SYS_RESOURCE}" ; then
  341.  echo "#define ${macro_prefix}HAVE_SYS_RESOURCE ${HAVE_SYS_RESOURCE}"
  342. else
  343.   cat >dummy.c <<!EOF!
  344. #include <sys/time.h>
  345. #include <sys/resource.h>
  346.   int main()
  347.   {
  348.     struct rusage res;
  349.     getrusage(RUSAGE_SELF, &res);
  350.     return (int)(res.ru_utime.tv_sec + (res.ru_utime.tv_usec / 1000000.0));
  351.   }
  352. !EOF!
  353.   # Note: We link because some systems have sys/resource, but not getrusage().
  354.   if ${CC} dummy.c >/dev/null 2>&1 ; then
  355.     echo "#define ${macro_prefix}HAVE_SYS_RESOURCE 1"
  356.   else
  357.     echo "#define ${macro_prefix}HAVE_SYS_RESOURCE 0"
  358.   fi
  359. fi
  360.  
  361. # check for sys/socket.h
  362.  
  363. if test -n "${HAVE_SYS_SOCKET}" ; then
  364.  echo "#define ${macro_prefix}HAVE_SYS_SOCKET ${HAVE_SYS_SOCKET}"
  365. else
  366.   echo '#include <sys/types.h>' >dummy.c
  367.   echo '#include <sys/socket.h>' >>dummy.c
  368.   if ${CC} -c dummy.c >/dev/null 2>&1 ; then
  369.     echo "#define ${macro_prefix}HAVE_SYS_SOCKET 1"
  370.   else
  371.     echo "#define ${macro_prefix}HAVE_SYS_SOCKET 0"
  372.   fi
  373. fi
  374.  
  375. # Check for a (Posix-compatible) sys/wait.h */
  376.  
  377. if test -n "${HAVE_SYS_WAIT}" ; then
  378.  echo "#define ${macro_prefix}HAVE_SYS_WAIT ${HAVE_SYS_WAIT}"
  379. else
  380.   cat >dummy.C <<!EOF!
  381.   extern "C" {
  382. #include <sys/types.h>
  383. #include <sys/wait.h>
  384.   }
  385.   int f() { int i; wait(&i); return i; }
  386. !EOF!
  387. #define ${macro_prefix}HAVE_UNION_WAIT 1"
  388.   if ${CXX} -c dummy.C >/dev/null 2>&1 ; then
  389.     echo "#define ${macro_prefix}HAVE_SYS_WAIT 1"
  390.   else
  391.     echo "#define ${macro_prefix}HAVE_SYS_WAIT 0"
  392.   fi
  393. fi
  394.  
  395. if test -n "${HAVE_UNISTD}" ; then
  396.  echo "#define ${macro_prefix}HAVE_UNISTD ${HAVE_UNISTD}"
  397. else
  398.   echo '#include <unistd.h>' >dummy.c
  399.   if ${CC} -c dummy.c >/dev/null 2>&1 ; then
  400.     echo "#define ${macro_prefix}HAVE_UNISTD 1"
  401.   else
  402.     echo "#define ${macro_prefix}HAVE_UNISTD 0"
  403.   fi
  404. fi
  405.  
  406. if test -n "${HAVE_DIRENT}" ; then
  407.  echo "#define ${macro_prefix}HAVE_DIRENT ${HAVE_DIRENT}"
  408. else
  409.   echo '#include <sys/types.h>
  410. #include <dirent.h>' >dummy.c
  411.   if ${CC} -c dummy.c >/dev/null 2>&1 ; then
  412.     echo "#define ${macro_prefix}HAVE_DIRENT 1"
  413.   else
  414.     echo "#define ${macro_prefix}HAVE_DIRENT 0"
  415.   fi
  416. fi
  417.  
  418. if test -n "${HAVE_CURSES}" ; then
  419.  echo "#define ${macro_prefix}HAVE_CURSES ${HAVE_CURSES}"
  420. else
  421.   echo '#include <curses.h>' >dummy.c
  422.   if ${CC} -c dummy.c >/dev/null 2>&1 ; then
  423.     echo "#define ${macro_prefix}HAVE_CURSES 1"
  424.   else
  425.     echo "#define ${macro_prefix}HAVE_CURSES 0"
  426.   fi
  427. fi
  428.  
  429. # There is no test for this at the moment; it is just set by the
  430. # configuration files.
  431. if test -n "${MATH_H_INLINES}" ; then
  432.   echo "#define ${macro_prefix}MATH_H_INLINES ${MATH_H_INLINES}"
  433. else
  434.   echo "#define ${macro_prefix}MATH_H_INLINES 0"
  435. fi
  436.  
  437. # Uncomment the following line if you don't have working templates.
  438. # echo "#define ${macro_prefix}NO_TEMPLATES"
  439.  
  440. rm -f dummy.C dummy.o dummy.c dummy.out TMP core a.out
  441.  
  442. echo "#endif /* !${macro_prefix}config_h */"
  443.